import { handlerGobackClick, hint, uploadFile } from "../../../utils/util";
import { Pets } from "../../../models/pets";

const pets = new Pets();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    actionType: "add",
    titleText: "新增宠物",
    id: "",
    //房屋地址
    houseId: [],
    houseIdName: [],
    //姓名
    name: "",
    //性别
    sex: "",
    sexName: "",
    //品种
    variety: "",
    //体型
    size: "",
    sizeName: "",
    //颜色
    color: "",
    colorName: "",
    //证号
    cardNumber: "",
    //时间
    time: "",
    //注射疫苗
    checked: true,
    //领养证
    license: "",
    //疫苗证
    vaccine: "",
    //宠物照片
    picture: "",
    titleNavName: "",
    minDate: new Date(2000, 0, 1).getTime(),
    maxDate: new Date().getTime(),
    currentDate: "12:00",
    currentDate1: "12:00",
    formatter(type, value) {
      if (type === "year") {
        return `${value}年`;
      } else if (type === "month") {
        return `${value}月`;
      }
      return value;
    },
    showVariety: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      this.setData({
        id: options.id,
        actionType: "edit",
        titleText: "编辑资料",
      });
      this.getById();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},
  onChange(e) {
    let key = e.currentTarget.dataset.key;
    let value = e.detail;
    this.setData({
      [key]: value,
    });
  },
  handlerGobackClick,
  //照片
  upload() {
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ["original", "compressed"], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ["album", "camera"], // 可以指定来源是相册还是相机，默认二者都有
      success: (res) => {
        const src = res.tempFilePaths[0];
        uploadFile({
          url: "/mini/peg/uploadPegPics",
          name: "file",
          file: src,
        })
          .then((res) => {
            this.setData({
              picture: res.result,
            });
          })
          .catch((err) => {
            console.log(err);
          });
      },
    });
  },
  //领养
  upload1() {
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ["original", "compressed"], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ["album", "camera"], // 可以指定来源是相册还是相机，默认二者都有
      success: (res) => {
        const src = res.tempFilePaths[0];
        uploadFile({
          url: "/mini/peg/uploadAdoptionPics",
          name: "file",
          file: src,
        })
          .then((res) => {
            this.setData({
              license: res.result,
            });
          })
          .catch((err) => {
            console.log(err);
          });
      },
    });
  },
  //疫苗
  upload2() {
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ["original", "compressed"], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ["album", "camera"], // 可以指定来源是相册还是相机，默认二者都有
      success: (res) => {
        const src = res.tempFilePaths[0];
        uploadFile({
          url: "/mini/peg/uploadVaccinePic",
          name: "file",
          file: src,
        })
          .then((res) => {
            this.setData({
              vaccine: res.result,
            });
          })
          .catch((err) => {
            console.log(err);
          });
      },
    });
  },
  goSelect(e) {
    let { type, id, code } = e.currentTarget.dataset;

    if (type === "petVariety") {
      if (!this.data.kind) {
        hint("请选择宠物种类");
        return;
      }
    }
    if (type === "petType") {
      if (!this.data.variety) {
        hint("请选择宠物品种");
        return;
      }
    }
    console.log(type);
    console.log(id);
    wx.navigateTo({
      url: `/owner-app/pages/select-community/index?type=${type}&phone=${this.data[id]}&code=${this.data[code]}`,
    });
  },
  goApply() {
    if (this.data.houseId.length === 0) {
      hint("请选择房屋地址");
      return;
    }
    if (this.data.name === "") {
      hint("请填写宠物姓名");
      return;
    }
    if (this.data.sex === "") {
      hint("请选择宠物性别");
      return;
    }
    if (this.data.variety === "") {
      hint("请填写宠物品种");
      return;
    }
    if (this.data.size === "") {
      hint("请选择宠物体型");
      return;
    }
    if (this.data.color === "") {
      hint("请选择宠物颜色");
      return;
    }
    if (this.data.cardNumber === "") {
      hint("请填写领养证号");
      return;
    }
    if (this.data.time === "") {
      hint("请选择领养时间");
      return;
    }
    if (this.data.checked) {
      if (this.data.vaccine === "") {
        hint("请上传疫苗证");
        return;
      }
    }
    if (this.data.license === "") {
      hint("请上传领养证");
      return;
    }

    if (this.data.picture === "") {
      hint("请上传宠物照片");
      return;
    }
    if (this.data.actionType === "add") {
      pets
        .add(
          // this.data.houseId.join(","),
          this.data.houseId,
          //姓名
          this.data.name,
          //性别
          this.data.sex,
          //品种
          this.data.variety,
          //体型
          this.data.size,
          //颜色
          this.data.color,
          //证号
          this.data.cardNumber,
          //时间
          this.data.time,
          //注射疫苗
          this.data.checked ? 0 : 1,
          //领养证
          this.data.license,
          //疫苗证
          this.data.vaccine,
          //宠物照片
          this.data.picture
        )
        .then((res) => {
          hint(res.message);
          wx.navigateTo({
            url: "/owner-app/pages/pet/index",
          });
        })
        .catch((err) => {
          console.log(err);
        });
    } else {
      pets
        .edit(
          this.data.id,
          // this.data.houseId.join(","),
          this.data.houseId,
          //姓名
          this.data.name,
          //性别
          this.data.sex,
          //品种
          this.data.variety,
          //体型
          this.data.size,
          //颜色
          this.data.color,
          //证号
          this.data.cardNumber,
          //时间
          this.data.time,
          //注射疫苗
          this.data.checked ? 0 : 1,
          //领养证
          this.data.license,
          //疫苗证
          this.data.vaccine,
          //宠物照片
          this.data.picture
        )
        .then((res) => {
          hint(res.message);
          wx.navigateTo({
            url: "/owner-app/pages/pet-detail/index?id=" + this.data.id,
          });
        })
        .catch((err) => {
          console.log(err);
        });
    }
  },
  controllPopup() {
    this.setData({
      isShow: false,
    });
  },
  selectedTime(event) {
    console.log(event);
    let { key, value } = event.currentTarget.dataset;
    this.setData({
      [key]: !this.data[key],
      [value]: event.detail,
    });
    console.log(this.data.time);
  },
  selectTime() {
    this.setData({
      isShow: true,
    });
  },
  getById() {
    pets
      .getById(this.data.id)
      .then((res) => {
        let {
          houseId,
          name,
          sex,
          brend,
          type,
          typeValue,
          color,
          adoptId,
          adoptDate,
          vaccine,
          adoptionPic,
          vaccinePic,
          pic,
          sexValue,
          colorValue,
          houseAddress,
        } = res.result;

        this.setData({
          houseId: houseId.split(","),
          houseIdName: houseAddress.split(","),
          //姓名
          name: name,
          //性别
          sex: sex,
          sexName: sexValue,
          //品种
          variety: brend,
          //体型
          size: type,
          sizeName: typeValue,
          //颜色
          color: color,
          colorName: colorValue,
          //证号
          cardNumber: adoptId,
          //时间
          time: adoptDate,
          //注射疫苗
          checked: vaccine === 0 ? true : false,
          //领养证
          license: adoptionPic,
          //疫苗证
          vaccine: vaccinePic,
          //宠物照片
          picture: pic,
        });
      })
      .catch((err) => {
        console.log(err);
      });
  },
  onChangeSwitch({ detail }) {
    // 需要手动对 checked 状态进行更新
    this.setData({ checked: detail });
  },
  goSelectHouse() {
    wx.navigateTo({
      url: "/owner-app/pages/select-house/index?id=" + this.data.houseId,
      // "/owner-app/pages/select-house/index?id=" + this.data.houseId.join(","),   //可多选的不要删
    });
  },
  goSelectSize() {
    wx.navigateTo({
      url: "/owner-app/pages/select-pet-size/index?id=" + this.data.size,
    });
  },
  goSelectColor() {
    this.setData({
      titleNavName: "宠物颜色",
    });
    wx.navigateTo({
      url:
        "/owner-app/pages/select-pet-color/index?id=" +
        this.data.color +
        "&titleNavName=" +
        this.data.titleNavName,
    });
  },
  onClose() {
    this.setData({ showVariety: false });
  },
  showVarietyDialog() {
    this.setData({ showVariety: true });
  },
  goSelectVariety() {},
});
